/**************************************************************************************
*
*	Copyright (C) 2010 Brent Andrew O. Chua - See LICENSE.txt for more info.
*		
*	Filename	-	Renderer.h
*
*	Description -	
*
*	Comments	-	We'll be using DX9 for this renderer. We need to abstract 
*						this in the future.
*
*	Modification History:
*			Name			   Date					Description
*			CodeSushi	-	10/15/2010	-	Creation of this file
**************************************************************************************/

#ifndef _PSX_RENDERER_H_
#define _PSX_RENDERER_H_

#include "PulseSTD.h"
#include "GraphicsTypes.h"
#include "RendererDevice.h"
#include "Singleton.h"
#include "Matrix4x4.h"
#include "Color.h"
#include "Array.h"

#include <D3D9.h>

namespace Pulse
{

	typedef Array< DisplayMode > DisplayModeList;

	class Renderer : public Singleton< Renderer >
	{
	public:

		virtual ~Renderer( void );

		virtual EErrorCode::Type Initialize( void );

		virtual void Cleanup( void );

		virtual void CreateDevice( void );

		// Release resources when device is lost
		virtual void OnDeviceLost( void );

		// Restore resources when device has been reset
		virtual void OnDeviceReset( void );

		// This is provided in cases where we need to change the 
		//	device or resolution.
		virtual void Reset( void );

		virtual void BeginRender( void );

		virtual void Render( void );

		virtual void EndRender( void );

		virtual void ClearBuffer( DWORD buffer );

		//virtual void ShowCursor( BOOL bShow );

		void SortRenderingStages( void );
		
		DisplayModeList * GetDisplayModeList( void );

		void SetDisplayMode( DisplayMode *pMode ) { m_displayMode = *pMode; }

		DisplayMode * GetDisplayMode( void ) { return &m_displayMode; }

		RendererDevice * GetDevice( void ) { return m_pDevice; }

		void * GetNakedDevice( void ) { return m_pDevice->GetDevice(); }

	private:

		friend Singleton;

		Renderer( void );

		void InitializeRendering( void );

		// Enumerate all available display modes in each adapter.
		virtual void BuildDisplayModeList( void );

		virtual void PerformRenderStages( void );

	private:

		typedef Array< RenderStage * > RenderStageList;

		D3DPRESENT_PARAMETERS	m_D3DParams;

		IDirect3D9				*m_pD3D;

		IDirect3DDevice9		*m_pD3DDevice;

		DisplayModeList			m_displayModes;

		DisplayMode				m_displayMode;

		RendererDevice			*m_pDevice;

		RenderStageList			m_renderStages;

		Texture					*m_pBackBuffer;

		IDirect3DSwapChain9		*m_pSwapChain;

		Color					m_clearColor;

	};

	PSX_INLINE DisplayModeList * Renderer::GetDisplayModeList( void )
	{
		return &m_displayModes;
	}


}

#endif /* _PSX_RENDERER_H_ */